package com.controller;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.pojo.*;
import com.service.OrderProjectService;
import com.service.OrderService;
import com.service.ProjectService;
import com.service.ReturnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class OrderController {
    @Autowired
    private ReturnerService returnerService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderProjectService orderProjectService;
    /**
     * 根据项目id来查询回报内容，将结果以list集合的形式返回
     * @param projectId
     * @return
     */
    @RequestMapping("/selectAllreturner")
    public Object selectAllreturner(long projectId){
        List<Returner> returners = returnerService.selectReturnByprojectid(projectId);
        return returners;
    }
    @RequestMapping("/selectByreturnid")
    public Object selectByreturnid(long id){
        //在业务层进行回报信息展示的处理
        OrderProject orderProject = orderProjectService.selectByRetrunId(id);
        return orderProject;
    }

    @RequestMapping("/selectAllOrder")
    public Object selectAllOrder(@RequestParam(defaultValue = "")String ispay,long userId,@RequestParam(defaultValue = "1") String pageNum){
        PageInfo<Order> orderPageInfo = orderService.selectAllOrder(Integer.parseInt(pageNum), ispay , userId);
        return orderPageInfo;
    }

    /**
     * 将前端传来的订单信息插入数据库，生成一个OrderId返回到控制成
     * 根据将返回的orderId设置给OrderProject
     * 并将OrderProject插入数据库
     * @param order
     * @param orderProject
     * @return
     */
    @RequestMapping("/inserOrder")
    public Object inserOrder(Order order, OrderProject orderProject){
        int i = orderService.insertOrder(order);
        orderProject.setOrderId(order.getId());
        int i1 = orderProjectService.insertOrderProject(orderProject);
        R r = new R();
        if (i==1){
            r.setCode("200");
            r.setMess("生成订单成功");
            r.setT(order);
        }else{
            r.setCode("400");
            r.setMess("添加失败");
        }
        return r;
    }

    /**
     * 根据订单id来删除订单以及订单项目表
     * @param orderId
     * @return
     */
    @RequestMapping("/deleteOrder")
    public Object deleteOrder(long orderId){
        int i = orderService.deleteOrderProject(orderId);
        R r;
        if (i==1){
             r= new R("删除成功","200",null);
        }else {
             r= new R("删除失败","200",null);
        }
        return r;
    }
    @RequestMapping("/startOrder")
    public Object startOrder(HttpServletRequest request){
        String order = request.getParameter("order");
        Order order1 = JSONArray.parseObject(order, Order.class);
        System.out.println(order1);
        UUID uuid = UUID.randomUUID();
        String s = uuid.toString().replace("-","");
        order1.setSupportDate(new Date());
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");
//        String format = sdf.format(new Date());
        order1.setOrderNum(s);
        int i = orderService.startOrder(order1);
        String order_project1 = request.getParameter("order_project");
        OrderProject orderProject = JSONArray.parseObject(order_project1, OrderProject.class);
        orderProject.setOrderId(order1.getId());
        orderProjectService.insertOrderProject(orderProject);
        return order1;
    }
    @RequestMapping("/updataOrderStatus")
    public Object updataOrderStatus(long id){
        UUID uuid = UUID.randomUUID();
        String s=uuid.toString().replace("-","");
        return orderService.updataOrderStatus(id,s);
    }

    @RequestMapping("/selectOrderById")
    public Object selectOrderById(long id){
        //根据订单id 查询订单信息
        Order order = orderService.selectOrderById(id);
        return order;
    }
}
